package com.dyzs.client;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import com.dyzs.util.Constants;
import com.dyzs.util.WSUtils;

public class GameClient {


	public String updateGame(String type,String pageNum) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", type);
		params.put("arg1", pageNum);			
		try {
			String resultXml = WSUtils.callWS("getGameJokService",params,Constants.ToolURL);			
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw  e;
		}
	}


	
	public Map<String,Object> pullXml(String resultXml) throws Exception {		
		if(resultXml.equals("W")){
			throw new Exception("服务端异常！");
		}else{
		
		List<String> group=null;
	    List<List<String>> child=null;	    
	    Map<String,Object> maps=null;
		
		try {
			ByteArrayInputStream bin = new ByteArrayInputStream(resultXml.getBytes());
			InputStreamReader in = new InputStreamReader(bin);
			XmlPullParser parser = Xml.newPullParser();
			parser.setInput(in);
			int event = parser.getEventType();
			while (event != XmlPullParser.END_DOCUMENT) {				
				switch (event) {
				case XmlPullParser.START_DOCUMENT:
					maps=new HashMap<String,Object>();
					break;
				case XmlPullParser.START_TAG:
					if ("totalNum".equals(parser.getName())) {
						maps.put("totalNum", parser.nextText());
					}else
					if ("lists".equals(parser.getName())) {
						group = new ArrayList<String>();			    	
				    	child = new ArrayList<List<String>>();
					}else
					
						if ("title".equals(parser.getName())) {
							group.add(parser.nextText());
						} else if ("content".equals(parser.getName())) {
							List childList=new  ArrayList<String>();
							childList.add(parser.nextText());
							child.add(childList);
						}					
					break;
				case XmlPullParser.END_TAG:
					if ("lists".equals(parser.getName())) {
						maps.put("group", group);
						maps.put("child", child);
					}
					break;
				}
				event = parser.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("解析失败！");
		}
		return maps;
	}
	}
}
